package net.anzhuo.common.config;

import java.net.InetAddress;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ES配置
 * 
 * @author fengzhichao
 *
 */
@Configuration
public class EsConfig {

	@Value("${spring.data.elasticsearch.cluster-nodes}")
	private String clusterNodes;

	@Value("${spring.data.elasticsearch.cluster-name}")
	private String clusterName;

	@SuppressWarnings("resource")
	@Bean
	public TransportClient transportClient() throws Exception {
		String InetSocket[] = clusterNodes.split(":");
		String address = InetSocket[0];
		Integer port = Integer.valueOf(InetSocket[1]);
	    return new PreBuiltXPackTransportClient(Settings.builder()
	    		.put("cluster.name", clusterName) //设置ES实例的名称
                .put("xpack.security.user","elastic:AnZhuo999")
                .put("client.transport.sniff", false) //自动嗅探整个集群的状态，把集群中其他ES节点的ip添加到本地的客户端列表中
                .build())
	    		.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(address), port));
	}
}
